//
//  PageViewController.m
//  DaDa
//
//  Created by User on 15/6/2.
//  Copyright (c) 2015年 User. All rights reserved.
//

#import "PageViewController.h"

@interface PageViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *pageImageView;

@property (nonatomic, strong) UIScrollView *imageScroll;

@property (nonatomic, assign) CGFloat _lastSize;

@property (nonatomic, strong) UILabel *pageNumberLab;

@property (nonatomic, strong) UIImageView *defaultImageView;

@end

@implementation PageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.imageScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT)];
    [self.view addSubview:self.imageScroll];
    self.imageScroll.maximumZoomScale=2.0;
    self.imageScroll.minimumZoomScale=1.0f;
    self.imageScroll.delegate=self;
    self.imageScroll.showsVerticalScrollIndicator=FALSE;
    self.imageScroll.showsHorizontalScrollIndicator=FALSE;
    self.imageScroll.scrollEnabled = FALSE;
    
    _pageImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _pageImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageScroll.contentSize=CGSizeMake(SCREEN_WIDTH, _pageImageView.bounds.size.height);
    CGFloat offsetX = (_imageScroll.bounds.size.width > _imageScroll.contentSize.width)?(_imageScroll.bounds.size.width - _imageScroll.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (_imageScroll.bounds.size.height > _imageScroll.contentSize.height)?(_imageScroll.bounds.size.height - _imageScroll.contentSize.height)/2 : 0.0;
    _pageImageView.center = CGPointMake(_imageScroll.contentSize.width/2 + offsetX,_imageScroll.contentSize.height/2 + offsetY);
    
    [self.imageScroll addSubview:_pageImageView];
    
    _pageNumberLab = [[UILabel alloc]initWithFrame:CGRectMake(0.0f, _pageImageView.frame.size.height - 35.0f, SCREEN_WIDTH, 20.0f)];
    _pageNumberLab.backgroundColor = [UIColor clearColor];
    _pageNumberLab.font = [UIFont systemFontOfSize:15.0f];
    _pageNumberLab.textColor = [UIColor hexStringToColor:COLOR_VALUE];
    _pageNumberLab.textAlignment = NSTextAlignmentCenter;
    _pageNumberLab.text = [NSString stringWithFormat:@"%ld / %ld",(long)(_indexInt + 1),(long)_accountInt];
    [self.view addSubview:_pageNumberLab];
    
    _defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, (SCREEN_HEIGHT - SCREEN_WIDTH * 0.6f - 128.0f) / 2.0f, SCREEN_WIDTH, SCREEN_WIDTH * 0.6f)];
    _defaultImageView.image = [UIImage imageNamed:@"default"];
    [self.view addSubview:_defaultImageView];
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _pageImageView;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    _pageImageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [_pageImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGE_URL_TEST,_urlString]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cachType, NSURL *imageUrl){
        if(!error)
        {
            _pageImageView.image = image;
            [_defaultImageView removeFromSuperview];
            _defaultImageView = nil;
            self.imageScroll.scrollEnabled = TRUE;
        }
        else
        { 
            UIWindow *window = [[UIApplication sharedApplication]keyWindow];
            
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:TRUE];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"图片无效";
            hud.labelFont = [UIFont systemFontOfSize:13.0f];
            [hud hide:TRUE afterDelay:2.0f];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
